<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">织物管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick">织物详情查看</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex">
          <!-- <el-button v-allow="'020101'" type="primary" @click="handleAdd()"
            >添加赛区信息</el-button
          > -->
        </div>
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-input maxlength="50" show-word-limit placeholder="请输入织物名称或编号" v-model="searchForm.keyword" clearable></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker v-model="date" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" @change="handleDate" clearable> </el-date-picker>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.state" placeholder="请选择织物状态" @change="onSubmit" clearable>
              <el-option v-for="item in clothStateArray" :key="item.key" :label="item.value" :value="item.key"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="operation" slot-scope="scope">
          <span @click="handleEdit(scope.row)" class="nx-btn nx-edit">查看</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog :addEditId="addEditId" :epc="epc" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog"></AddEditDialog>
  </div>
</template>
<script>
import NxTable from "@/components/NxTable.vue";
import AddEditDialog from "./components/addEditDialog.vue";
import { Page } from "@/utils/pagination";
import { getCloth, getClothState } from "@/api/clothes";

export default {
  components: {
    NxTable,
    AddEditDialog,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        keyword: "",
        state: "",
        startdate: "",
        enddate: "",
      },
      pagesForm: {
        page: 1,
        limit: 10,
      },
      // 表格字段
      tableCols: [
        {
          label: "织物编码",
          prop: "rfid",
          align: "center",
        },
        {
          label: "织物名称",
          prop: "name",
          align: "center",
        },
        {
          label: "当前位置",
          prop: "state",
          align: "center",
        },
        {
          label: "织物循环次数",
          prop: "cleantimes",
          align: "center",
        },
        {
          label: "更新时间",
          prop: "update_time",
          align: "center",
        },
        {
          label: "全流程追溯",
          slot: "operation",
          align: "center",
          width: 180,
        },
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: "", // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: "add",
      addEditId: "", // 添加编辑rowid
      clothStateArray: [], // 织物状态列表
      epc: "", // epc
      date: [], // 日期范围
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    // // 初始化日期
    // this.formatteDate();
    // // 初始化日期（传值）
    // if (this.date && this.date.length == 2) {
    //   this.searchForm.startdate = this.date[0];
    //   this.searchForm.enddate = this.date[1];
    // }
  },
  // 初始化加载
  mounted() {
    // 查询
    this.getList();

    // 获取织物状态
    this.getClothState();
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.page = 1;
      this.pagesForm.page = 1;
      this.getList();
    },
    // 分页查询
    refresh(val) {
      this.pagesForm.page = val.page;
      this.pagesForm.limit = val.limit;
      this.getList();
    },
    // 重置
    resetForm() {
      this.searchForm = {
        state: "",
        startdate: "",
        enddate: "",
      };
      this.pagesForm = {
        page: 1,
        limit: 10,
      };

      this.date = [];

      this.onSubmit();
    },
    // 查询列表
    getList() {
      getCloth(this.pagesForm, this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.pagination.count = res.count;
        }
      });
    },
    // 获取织物状态
    getClothState() {
      getClothState().then((res) => {
        if (res.code == 200) {
          this.clothStateArray = res.data;
        }
      });
    },
    // 添加
    handleAdd() {
      this.addEditId = "";
      this.addEditStatus = "add";
      this.addEditIsDialog = true;
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + "";
      this.epc = row.rfid;
      this.addEditStatus = "edit";
      this.addEditIsDialog = true;
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false;
      this.typeDialog = false;
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false;
      this.addEditIsDialog = false;
    },
    // 初始格式化日期
    formatteDate() {
      let date = [new Date(new Date().getFullYear(), new Date().getMonth(), 1), new Date()];
      const formattedDates = date.map((date) => {
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, "0");
        const day = date
          .getDate()
          .toString()
          .padStart(2, "0");
        return `${year}-${month}-${day}`;
      });
      this.date = formattedDates;
    },
    // 日期切换
    handleDate() {
      if (this.date && this.date.length == 2) {
        this.searchForm.startdate = this.date[0];
        this.searchForm.enddate = this.date[1];
      } else {
        this.searchForm.startdate = "";
        this.searchForm.enddate = "";
      }

      this.onSubmit();
    },
  },
};
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
